<?php

require_once WACT_ROOT . 'controller/controller.inc.php';

require_once WACT_ROOT . 'controller/form.inc.php';
//require_once WACT_ROOT . 'util/dataspace.inc.php';
require_once APP_ROOT . "view/common/layout.view.php";
require_once APP_ROOT . "control/pub/pub.index.php";
require_once APP_ROOT . "model/common/user.php";
require_once APP_ROOT . "model/common/session.php";
require_once APP_ROOT . "model/common/parameters.php";
require_once APP_ROOT . "model/common/price.php";
require_once APP_ROOT . "model/common/site.php";


class SubscribeView extends LayoutView {

    function SubscribeView($TemplateFile) {
        parent :: LayoutView($TemplateFile);
    }

    function prepare() {
    	parent::prepare();
		$this->Template->set('SITE_CSS',Site::getSiteTemplateCSS());
		$this->Template->set('SITE_BASE',Site::getSiteTemplateBase());
		$this->Template->set('SITE_TITLE',Site::getTitle());
		$this->Template->set('title',Site::getTitle().' - Login');
    	if(APP_NAME!="") {
			$this->Template->set('app_name',APP_NAME);
		} else $this->Template->set('app_name','.');

        $site_name = Site::getTitle();
 
     	$this->Template->set('title',$site_name);
    	 
     	$mode = Parameters::getRunningMode();
     	if ($mode) $paypal_url = 'https://www.paypal.com/cgi-bin/webscr';
     	else  $paypal_url = 'https://www.sandbox.paypal.com/cgi-bin/webscr';
     	$this->Template->set('paypal_url',$paypal_url);

     	$paypal_email = Parameters::getPaypalEmail();
     	$this->Template->set('paypal_email',$paypal_email);

     	$return_url = HOST.'/'.APP_NAME.'/'.'thanksubscribe.htm';
        $cancel_url = HOST.'/'.APP_NAME.'/'.'cancelsubscribe.htm';
        $ipn_url = HOST.'/'.APP_NAME.'/'.'paypal.php';
        $this->Template->set('return_url',$return_url);
        $this->Template->set('cancel_url',$cancel_url);
        $this->Template->set('ipn_url',$ipn_url);

        $image_url = HOST.'/'.APP_NAME.'/images/'.'logo.jpg';
        $this->Template->set('image_url',$image_url);

        $logged = Session::isLogin();
        $user_id = 0;
        if ($logged) $user_id = Session::getUserId();
        $this->Template->set('user_id',$user_id);

        $price1 = number_format(Price::getPrice(1),2);
        $price2 = number_format(Price::getPrice(2),2);
        $price3 = number_format(Price::getPrice(3),2);
        $price4 = number_format(Price::getPrice(4),2);

        $this->Template->set('price1',$price1);
        $this->Template->set('price2',$price2);
        $this->Template->set('price3',$price3);
        $this->Template->set('price4',$price4);

        if ($user_id) {
        	$record =& User::getRecord($user_id);
            if ($record) {
               $this->Template->set('firstname',$record->get('firstname'));
               $this->Template->set('lastname',$record->get('lastname'));
               $this->Template->set('address1',$record->get('address1'));
               $this->Template->set('address2',$record->get('address2'));
               $this->Template->set('city',$record->get('city'));
               $this->Template->set('state',$record->get('state'));
               $this->Template->set('zip',$record->get('zip'));
            }
        }

    }
}



class SubscribePage extends PageController {

    function SubscribePage() {
        parent::PageController();

      /*  $Form = new FormController();
		$this->registerOnLoadListener(new Delegate($this, 'act'));
        $this->addChild('login_form', $Form);*/

        $site_id = Site::getTemplateSiteID();

    	$mytemplate = 'pub/subscribe.html';
     
        $mytemplate1 = 'pub/already_subscriber.html';
     
        $mytemplate2 = 'pub/cancel_subscriber.html';
  

        $this->setDefaultView(new SubscribeView($mytemplate ));
        $this->addView('success', WACT_DEFAULT_VIEW);
        $this->addView('login', new Handle('RedirectView', array('/login')));
        $this->addView('register', new Handle('RedirectView', array('/register')));
        $this->addView('thanks', new Handle('RedirectView', array('/thanksubscribe')));
        $this->addView('already', new Handle('SubscribeView', array($mytemplate1)));
        $this->addView('cancel', new Handle('SubscribeView', array($mytemplate2)));

        $this->registerOnActivateListener(new Delegate($this, 'check'));
    }

    function check(&$source, &$request, &$responseModel) {

        //return 'success';

		$is_logged = Session::isLogin();
		if  (!$is_logged) return 'register';

        $user_id = Session::getUserId();
        if ($user_id) $is_subscriber = User::isUserSubscriptionActive($user_id);

        if  ($is_subscriber) return 'already';

		return 'success';

	}

}

class ThanksubscribeView extends LayoutView {

    function ThanksubscribeView($TemplateFile) {
        parent :: LayoutView($TemplateFile);
    }

    function prepare() {
    	parent::prepare();
		$this->Template->set('SITE_CSS',Site::getSiteTemplateCSS());
		$this->Template->set('SITE_BASE',Site::getSiteTemplateBase());
		$this->Template->set('SITE_TITLE',Site::getTitle());
		$this->Template->set('title',Site::getTitle().' - Thanks for subscription');
       
    }
}

class CancelsubscribeView extends LayoutView {

    function CancelsubscribeView($TemplateFile) {
        parent :: LayoutView($TemplateFile);
    }

    function prepare() {
    	parent::prepare();
  		$this->Template->set('SITE_CSS',Site::getSiteTemplateCSS());
		$this->Template->set('SITE_BASE',Site::getSiteTemplateBase());
		$this->Template->set('SITE_TITLE',Site::getTitle());
		$this->Template->set('title',Site::getTitle().' - Your subscription process has not been finished!');
     }
}

class AlreadysubscribeView extends LayoutView {

    function AlreadysubscribeView($TemplateFile) {
        parent :: LayoutView($TemplateFile);
    }

    function prepare() {
    	parent::prepare();
        $this->Template->set('SITE_CSS',Site::getSiteTemplateCSS());
		$this->Template->set('SITE_BASE',Site::getSiteTemplateBase());
		$this->Template->set('SITE_TITLE',Site::getTitle());
		$this->Template->set('title',Site::getTitle().' - You are already a subscriber!');      

    }
}



class ThanksubscribePage extends PageController {

    function ThanksubscribePage() {
    	parent::PageController();
    	$site_id = Site::getTemplateSiteID();

    	$mytemplate = 'pub/thanksubscribe.html';
 

        $this->setDefaultView(new ThanksubscribeView($mytemplate));

    }

}

class CancelsubscribePage extends PageController {

    function CancelsubscribePage() {
        parent::PageController();
        $site_id = Site::getTemplateSiteID();

        $mytemplate = 'pub/cancel_subscriber.html';
   

        $this->setDefaultView(new CancelsubscribeView($mytemplate));

    }

}

class AlreadysubscribePage extends PageController {

    function AlreadysubscribePage() {
        parent::PageController();


        $mytemplate = 'pub/already_subscriber.html';


        $this->setDefaultView(new AlreadysubscribeView($mytemplate));

    }

}

?>
